﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.MODEL.Custom_Model;
using OLTS.WEB.BaseClass;

namespace ONTS.WEB.Service
{
    /// <summary>
    /// Summary description for TimeSheetService
    /// </summary>
    public class TimeSheetService : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            var model = new OLTS.MODEL.Custom_Model.OTS_TIME_SHEET();
            var child = new OLTS.MODEL.OTS_WORK_DETAIL();
            var childlst = new List<OLTS.MODEL.OTS_WORK_DETAIL>();
            var currentuser = BaseService.LOGIN.EMPLOYEE_ID;

            if (request["oper"] == "add")
            {
                var rowix = DataSource.Count;
                child.SEQ = rowix + 1;
                child.AMOUNT_OF_TIME = Convert.ToInt16(request["HOURS"]);
                child.AMOUNT_OF_TIME_OVER_TIME = Convert.ToInt16(request["OT"]);
                child.DETAIL = request["DETAIL"];
                child.CREATED = DateTime.Now;
                child.CREATE_BY = BaseService.LOGIN.EMPLOYEE_ID;
                child.Action = OLTS.MODEL.ActionEnum.Create;
                DataSource.Add(child);
                 
                //var cmd = new TimeSheet_Service(model);
                //cmd.Add();
            }
            else
            {
                if (request["oper"] == "del")
                {
                    var rowix = Convert.ToInt32(request["SEQ"]);
                    var modeldel = DataSource.Where(x => x.SEQ == rowix).FirstOrDefault();
                    if (modeldel != null)
                    {
                        modeldel.Action = OLTS.MODEL.ActionEnum.Delete;
                    }
                }
                else if (request["oper"] == "edit")
                {
                    var rowix = Convert.ToInt32(request["SEQ"]);
                    var modeledit = DataSource.Where(x => x.SEQ == rowix).FirstOrDefault();
                    if (modeledit != null)
                    {
                        modeledit.AMOUNT_OF_TIME = Convert.ToInt16(request["HOURS"]);
                        modeledit.AMOUNT_OF_TIME_OVER_TIME = Convert.ToInt16(request["OT"]);
                        modeledit.DETAIL = request["DETAIL"];
                    }
                }
            }

            var _cmd = new TimeSheet_Service(BaseService.LOGIN.EMPLOYEE_ID, Convert.ToInt16(request["MONTH"]));
            var list = new List<OLTS.MODEL.OTS_TIME_SHEET>();
            if (request["MONTH"] != null)
            {
               
                var lstTimesheet = _cmd.GetAllinclude();

                foreach (var d in _cmd.GetAllinclude())
                {
                    foreach (var ch in d.OTS_WORK_DETAIL.ToList())
                    {
                        var obj = new OLTS.MODEL.OTS_TIME_SHEET();
                          
                        list.Add(obj);
                    }

                }

            }
            list = list.OrderBy(o => o.ID).ToList();

            int total = list.Count == 0 ? 1 : (int)Math.Ceiling((double)list.Count / 10);

            var jsonData = new
            {
                total = total,
                page = request["page"],
                records = list.Count,
                rows = list.Skip(Convert.ToInt16(request["page"]) * 10 - 10).Take(10)
            };

            response.Write(new JavaScriptSerializer().Serialize(jsonData));
        }

        public List<OLTS.MODEL.OTS_WORK_DETAIL> DataSource
        {
            get
            {
                var d = (List<OLTS.MODEL.OTS_WORK_DETAIL>)HttpContext.Current.Session["details"];
                if (d == null)
                {
                    d = new List<OLTS.MODEL.OTS_WORK_DETAIL>();
                    HttpContext.Current.Session["details"] = d;
                }
                return d;
            }
            set
            {
                HttpContext.Current.Session["details"] = value;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}